package com.ir.slassm.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.datanucleus.util.StringUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ir.slassm.domain.PropertyManager;
import com.ir.slassm.domain.SLMessage;

public class MessengerServiceImpl extends RemoteServiceServlet {
	
	Log logger = LogFactory.getLog(MessengerServiceImpl.class.getName());

	private static final long serialVersionUID = -8498155518378954306L;
	
	public static final String MESSAGE_PARAMETER_NAME = "action";
	public static final String MESSAGE_ACTION_EXPIRE = "exp";
	public static final String MESSAGE_ACTION_RETRIEVE_MESSAGE = "ret";
	public static final String MESSAGE_ACTION_SEND_MESSAGE = "snd";
	public static final String MESSAGE_ACTION_PROPERTY_SAVE = "prm_sv";
	public static final String MESSAGE_ACTION_PROPERTY_GET = "prm_get";
	public static final String MESSAGE_ACTION_PROPERTY_DELETE = "prm_dl";
	
	private static SLMessage slMessage;
	private static PropertyManager propertyManager;

	@Override
	public void init() throws ServletException {
		super.init();
		if(slMessage == null) {
			slMessage = new SLMessage();
		}
		if(propertyManager == null) {
			propertyManager = new PropertyManager();
		}
	}
	
	
	@Override
	/**
	 * TODO: Should consider splitting out into seperate service calls instead of one service call to handle all requests
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		logger.info("doGet Enter"); 
		
		String action = req.getParameter(MESSAGE_PARAMETER_NAME);
		String results = "";

		if(slMessage.verifyApprovedRequestor(req.getRemoteAddr())) {
			if(MESSAGE_ACTION_SEND_MESSAGE.equals(action)) {
				String senderId = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_SENDER_ID);
				String receiverId = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_RECEIVER_ID);
				
				if(!slMessage.isSpamCall(req.getRemoteAddr(), senderId, receiverId)) {
					String messageHeader = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_MESSAGE_HEADER);
					String messageBody = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_MESSAGE_BODY);
					results = slMessage.sendMessage(req.getRemoteAddr(), senderId, receiverId, messageHeader, messageBody);
				}
			} else if (MESSAGE_ACTION_RETRIEVE_MESSAGE.equals(action)) {
				String senderId = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_SENDER_ID);
				String receiverId = req.getParameter(SLMessage.MESSAGE_ATTRIBUTE_RECEIVER_ID);
				
				if(!slMessage.isSpamCall(req.getRemoteAddr(), senderId, receiverId)) {
					results = slMessage.retrieveMessages(senderId, receiverId);
				}
			} else if(MESSAGE_ACTION_EXPIRE.equals(action)) {
				results = slMessage.clearExpiredMessages();
			} else if(MESSAGE_ACTION_PROPERTY_SAVE.equals(action)) {
				String group = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_GROUP);
				String name = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_NAME);
				String value = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_VALUE);
				results = propertyManager.saveProperty(group, name, value);
			} else if(MESSAGE_ACTION_PROPERTY_GET.equals(action)) {
				String group = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_GROUP);
				String name = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_NAME);
				if(StringUtils.isEmpty(name)) {
					results = propertyManager.findPropertyGroup(group);
				} else {
					results = propertyManager.findProperty(group, name);
				}
			} else if(MESSAGE_ACTION_PROPERTY_DELETE.equals(action)) {
				String group = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_GROUP);
				String name = req.getParameter(PropertyManager.PROPERTY_ATTRIBUTE_NAME);
				results = propertyManager.deleteProperty(group, name);
			}
		}
		resp.getOutputStream().print(results);
		
		logger.info("doGet Exit");
	}

}
